<?php

include ('../pages/check_login.include.php');

include ('../classes/autoloading_pages.include.php');

if (!$logged) {
    header('Location: /eventijesolo/login/');
    exit();
}

$hm = new HTMLElementsManager();
$navigation = $hm->get_HTML_element_by_name('navigation');
$footer = $hm->get_HTML_element_by_name('footer');

define (ACTION, 'action');
define (ACTION_EDIT, 'edit');
define (ACTION_NEW, 'new');
define (ACTION_SAVE, 'save');
define (ACTION_DELETE, 'delete');
define (NEWS_ID, 'id');

$through_controller = TRUE;
if (isset($_GET[ACTION])) {
    $nm = new NewsManager();
    if($_GET[ACTION] == ACTION_EDIT){
        if(isset($_GET[NEWS_ID])) {
            $news = $nm->get_news_by_id($_GET[NEWS_ID]);
        } else {
            echo 'error';
            exit();
        }
    } else if ($_GET[ACTION] == ACTION_SAVE) {
        if (isset($_GET[NEWS_ID])) {
            $news = $nm->get_news_by_id($_GET[NEWS_ID]);
            $news->title = (isset($_POST['title'])) ? $_POST['title'] : '';
            $news->content = (isset($_POST['content'])) ? $_POST['content'] : '';
            $news->link = (isset($_POST['link'])) ? $_POST['link'] : '';
            list($day, $month, $year) = explode('/', isset($_POST['publication_date']) ? $_POST['publication_date'] : date('d-m-Y'));
            list($hour, $minutes) = explode(':', isset($_POST['publication_time']) ? $_POST['publication_time'] : date('H:i'));
            $news->publication_timestamp = new DateTime($year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minutes . ':00');
            $news->image = (isset($_POST['image'])) ? $_POST['image'] : 'http://placehold.it/100x100';
            $news->priority = (isset($_POST['priority'])) ? $_POST['priority'] : 1;
            $nm->update_news($news);
        } else {
            $news = new News();
            $news->title = (isset($_POST['title'])) ? $_POST['title'] : '';
            $news->content = (isset($_POST['content'])) ? $_POST['content'] : '';
            $news->link = (isset($_POST['link'])) ? $_POST['link'] : '';
            list($day, $month, $year) = explode('/', isset($_POST['publication_date']) ? $_POST['publication_date'] : date('d-m-Y'));
            list($hour, $minutes) = explode(':', isset($_POST['publication_time']) ? $_POST['publication_time'] : date('H:i'));
            $news->publication_timestamp = new DateTime($year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minutes . ':00');
            $news->image = (isset($_POST['image'])) ? $_POST['image'] : 'http://placehold.it/100x100';
            $news->priority = (isset($_POST['priority'])) ? $_POST['priority'] : 1;
            $news->creator = $_SESSION['user'];
            $nm->save_news($news);
        }
        header('Location: /eventijesolo/');
        exit();
    } else if ($_GET[ACTION] == ACTION_DELETE) {
        if (isset($_GET[NEWS_ID])) {
            $nm->delete_news($_GET[NEWS_ID]);
            header('Location: /eventijesolo/');
            exit();
        }
    }
} else {
    echo 'Nessuna azione definita: errore!';
    exit();
}

include ('news.view.php');
?>
